package com.uzai.console.service.devicegroup.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.devicegroup.DeviceGroupIdDto;
import com.uzai.console.dto.devicegroup.DeviceGroupInfoDto;
import com.uzai.console.dto.devicegroup.DeviceGroupQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.devicegroup.DeviceGroupService;
import com.uzai.console.vo.devicegroup.DeviceGroupInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单状态
 * @author liuqi
 * @date 2021年08月19日 16:50
 */
@Service
public class DeviceGroupServiceImpl implements DeviceGroupService {

    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private JdTokenMapper jdTokenMapper;
    @Autowired
    private PddTokenMapper pddTokenMapper;
    @Autowired
    private VphTokenMapper vphTokenMapper;

    /**
     * 分页查询机器人分组分组列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceGroupQuery
     * @return DeviceGroupInfoVo
     */
    public Page<DeviceGroupInfoVo> findDeviceGroupList(DeviceGroupQuery deviceGroupQuery){
        //新建返回到页面的LIST对象
        List<DeviceGroupInfoVo> deviceGroupInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<DeviceGroup> page = new Page<>();
        deviceGroupQuery.convert(page);
        List<DeviceGroup> deviceGroupList = deviceGroupMapper.findByList(page, deviceGroupQuery);

        if(deviceGroupList != null){
            for (DeviceGroup deviceGroup : deviceGroupList){
                DeviceGroupInfoVo deviceGroupInfoVo = new DeviceGroupInfoVo();
                BeanUtils.copyProperties(deviceGroup, deviceGroupInfoVo);
                deviceGroupInfoVoList.add(deviceGroupInfoVo);
            }
        }

        //返回到页面分页对象
        Page<DeviceGroupInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(deviceGroupInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加机器人分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceGroupInfoDto
     * @return null
     */
    public void addDeviceGroup(DeviceGroupInfoDto deviceGroupInfoDto){
        if(StringUtils.isBlank(deviceGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        if(StringUtils.isBlank(deviceGroupInfoDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        //验证是否重名
        DeviceGroup deviceGroup_name = deviceGroupMapper.selectByName(deviceGroupInfoDto.getMerId(), deviceGroupInfoDto.getName(), deviceGroupInfoDto.getDeviceType());
        if(deviceGroup_name != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
        }

        Integer now = DateUtil.getNowTime();
        DeviceGroup deviceGroup = new DeviceGroup();
        deviceGroup.setId(IdWorker.getId());
        deviceGroup.setMerId(deviceGroupInfoDto.getMerId());
        deviceGroup.setName(deviceGroupInfoDto.getName());
        deviceGroup.setDeviceType(deviceGroupInfoDto.getDeviceType());
        deviceGroup.setCreateTime(now);
        deviceGroup.setUpdateTime(now);
        deviceGroupMapper.insert(deviceGroup);
    }

    /**
     * 删除机器人分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceGroupIdDto
     * @return null
     */
    public void deleteDeviceGroup(DeviceGroupIdDto deviceGroupIdDto){
        if(deviceGroupIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        //查询该分组是否已经被使用
        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
        deviceWechatInfoQuery.setMerId(deviceGroupIdDto.getMerId());
        deviceWechatInfoQuery.setGroupId(deviceGroupIdDto.getId());
        List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
        if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组已经被使用，不能删除");
        }
        deviceGroupMapper.deleteById(deviceGroupIdDto.getId(), deviceGroupIdDto.getMerId());
    }

    /**
     * 查询机器人分组详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupIdDto
     * @return null
     */
    public DeviceGroupInfoVo findDeviceGroupInfo(DeviceGroupIdDto deviceGroupIdDto){
        if(deviceGroupIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceGroupIdDto.getId(), deviceGroupIdDto.getMerId());
        if(deviceGroup == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组不存在");
        }

        DeviceGroupInfoVo deviceGroupInfoVo = new DeviceGroupInfoVo();
        BeanUtils.copyProperties(deviceGroup,deviceGroupInfoVo);

        return deviceGroupInfoVo;
    }

    /**
     * 修改机器人分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceGroupInfoDto
     * @return null
     */
    public void updateDeviceGroup(DeviceGroupInfoDto deviceGroupInfoDto){

        if(deviceGroupInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(StringUtils.isBlank(deviceGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceGroupInfoDto.getId(), deviceGroupInfoDto.getMerId());
        if(deviceGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组不存在");
        }

        //验证新名字是否已经存在
        if(!deviceGroupInfoDto.getName().equals(deviceGroup.getName())){ //表示修改了组名称
            //验证新名称是否存在
            DeviceGroup deviceGroup_name = deviceGroupMapper.selectByName(deviceGroupInfoDto.getMerId(), deviceGroupInfoDto.getName(), deviceGroup.getDeviceType());
            if(deviceGroup_name != null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
            }
        }

        Integer now = DateUtil.getNowTime();
        deviceGroup.setName(deviceGroupInfoDto.getName());
        deviceGroup.setUpdateTime(now);
        deviceGroupMapper.updateById(deviceGroup);
    }

}
